package com.songtai.dao.impl;

import java.io.Serializable;
import java.util.Set;

import org.hibernate.SessionFactory;

import com.songtai.dao.NodeDao;
import com.songtai.po.Node;

/**
 * @author zzy 节点操作Dao实现类
 */
public class NodeDaoImpl implements NodeDao, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SessionFactory sessionFactory;

	/**
	 * 增加节点
	 */
	@Override
	public void addNode(Node node) {
		sessionFactory.getCurrentSession().persist(node);
	}

	/**
	 * 获取制定节点的一级子节点
	 */
	@Override
	public Set<Node> getChildrens(Long nodeid) {
		Node node = (Node) sessionFactory.getCurrentSession().get(Node.class, nodeid);
		if (node != null) {
			return node.getChildrens();
		} else {
			return null;
		}
	}

	/**
	 * 获取兄弟节点
	 */
	@Override
	public Set<Node> getBrothers(Long nodeid) {
		Node node = (Node) sessionFactory.getCurrentSession().get(Node.class, nodeid);
		if (node != null) {
			Node parent = node.getParent();
			return parent.getChildrens();
		}
		return null;
	}

	@Override
	public Node getNodeById(Long nodeid) {
		return (Node) this.sessionFactory.getCurrentSession().get(Node.class, nodeid);
	}

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * @param sessionFactory
	 *            the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
